package com.jzg.mvc.biz.version;

import java.io.File;

import com.jfinal.core.Path;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;
import com.jzg.common.controller.BaseController;
import com.jzg.common.model.biz.Version;
import com.jzg.common.model.biz.VersionFiles;
import com.jzg.common.util.FileUtil;
import com.jzg.common.util.UuidUtil;

@Path(value = "/biz/version", viewPath = "/views/biz/version")
public class VersionController extends BaseController {
	
	public void index() {
		this.render("list.html");
	}

	public void listData() {
		int pageNumber = getParaToInt("page", 1);
		int pageSize = getParaToInt("limit", 10);
		String keyword = getPara("keyword");
		
		String sqlExceptSelect = " from " + Version.TABLE_NAME + " o  where 1=1 ";
		
		if (!StrKit.isBlank(keyword)) {
			sqlExceptSelect += " and ( o.number='"+keyword+"' or o.remark like '%" + keyword + "%' ) ";
		}
		
		sqlExceptSelect += "  order by   o.number desc ";
		Page<Version> page = Version.dao.paginate(pageNumber, pageSize, "select o.* ", sqlExceptSelect);
		renderLayuiPage(0, page.getList(), "", page.getTotalRow());
	}
	
	/***
	 * 删除
	 * @throws Exception
	 */
	public void delete() {
		Version model = Version.dao.findById(this.getPara("id"));
		if (model != null) {
			if (model.delete()) {
				renderSuccess("删除成功！");
			} else {
				renderError("删除失败！");
			}
		} else {
			renderError("数据不存在,请刷新后再试!");
		}
	}
	
	
	public void save() {
		Version model = getModel(Version.class);
		if (StrKit.notBlank(model.getId())) {
			if (model.update()) {
				renderSuccess("更新成功！");
			} else {
				renderError("更新失败！");
			}
		} else {
			model.setId(UuidUtil.getUUID());
			if (model.save()) {
				renderSuccess("保存成功！");
			} else {
				renderError("保存失败！");
			}
		}
	}
	
	public void listDataForFiles() {
		int pageNumber = getParaToInt("page", 1);
		int pageSize = getParaToInt("limit", 10);
		String versionId=this.getPara("versionId");
		String sqlExceptSelect = " from " + VersionFiles.TABLE_NAME +" where  versionId='"+versionId+"' ";
		Page<VersionFiles> page = VersionFiles.dao.paginate(pageNumber, pageSize, "select * ", sqlExceptSelect);
		renderLayuiPage(0, page.getList(), "", page.getTotalRow());
	}
	
	private String rootPath = PathKit.getWebRootPath();
	public void upload() {
		UploadFile uploadFile = getFile();
		try {
			String versionId=getPara("versionId");
			Version version=Version.dao.findById(versionId);
			if(version==null) {
				renderError("参数必填！");
				return ;
			}
			String path = "/upload/version" + "/"+versionId+"/" + uploadFile.getFileName().replace(" ", "");
			File file = new File(rootPath+path);
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			if (file.exists()) {
				file.delete();
			}
			uploadFile.getFile().renameTo(file);
			VersionFiles versionFiles = new VersionFiles();
			versionFiles.setId(UuidUtil.getUUID());
			versionFiles.setVersionId(version.getId());
			versionFiles.setFormat(FileUtil.lastName(file));
			versionFiles.setFileName(file.getName());
			versionFiles.setFilePath(path);
			versionFiles.setVersion(version.getNumber()+"");
			if(versionFiles.save()) {
				renderSuccess("上传成功！",versionFiles);
			}else {
				renderError("上传失败！");
			}
		} catch (Exception e) {
			renderError(e.getMessage());
		}
	}
	
	public void saveFiles() {
		VersionFiles model = getModel(VersionFiles.class);
		if (StrKit.notBlank(model.getId())) {
			if (model.update()) {
				renderSuccess("更新成功！");
			} else {
				renderError("更新失败！");
			}
		} else {
			model.setId(UuidUtil.getUUID());
			if (model.save()) {
				renderSuccess("保存成功！");
			} else {
				renderError("保存失败！");
			}
		}
	}
	
	public void deleteFiles() {
		VersionFiles model = VersionFiles.dao.findById(this.getPara("id"));
		if (model != null) {
			if (model.delete()) {
				renderSuccess("删除成功！");
				try {
					File file=new File(rootPath+model.getFilePath());
					if(file.exists()) {
						file.delete();
					}
				}catch (Exception e) {
				}
			} else {
				renderError("删除失败！");
			}
		} else {
			renderError("数据不存在,请刷新后再试!");
		}
	}
	
	
}
